#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
#define MAXDATA 100005  //大于堆中所有可能元素的值
#define MINDATA 0     //小于堆中所有可能元素的值
#define EROOR -1      //堆中不可能出现的值
typedef int ElementType;

typedef struct HNode *Heap;
//堆
struct HNode{
    ElementType *Data;//存储元素的数组
    int Size; //堆中当前元素个数 
    int Capacity;  //堆的最大容量 
};
typedef Heap MinHeap; //最小堆

//创建容量为Maxsize的最小堆
MinHeap CreateMinHeap(int MaxSize){
    MinHeap H = (MinHeap)malloc(sizeof(struct HNode));
    H->Data = (ElementType*)malloc(sizeof(ElementType)*(MaxSize+1));//从a[1]开始保存数 所以数组数量要+1
    H->Size = 0;
    H->Capacity=MaxSize;
    H->Data[0] = MINDATA;
    return H;
}
bool IsFull(MinHeap H)
{
    return(H->Size == H->Capacity);
}
bool IsEmpty(MinHeap H)
{
    return(H->Size==0);
}

//最小堆的删除
ElementType DeleteMin(MinHeap H){
    if(IsEmpty(H)){
        printf("EMPTY QUEUE!");
        return EROOR;
    }
    ElementType top = H->Data[1];
    ElementType x = H->Data[H->Size--];
    int parent,child;
    for (parent = 1; parent*2<H->Size; parent=child) {
        child = parent*2;
            //注意这里是存在右子节点 并且 右子节点比左子节点小    
        if (child!=H->Size && H->Data[child] > H->Data[child+1]) {
            child++;
            //如果比右子节点还小
            if (H->Data[child]>=x) {
                break;
            }else{//下滤
                H->Data[parent] = H->Data[child];
            }
        }
    }
    H->Data[parent] = x;
    return top;
}

//最小堆的插入
bool InsertMin(MinHeap H, ElementType x){
    if(IsFull(H)){
        printf("Minheap is full");
    }
    int p = ++H->Size;
    for (; H->Data[p/2]>x; p/=2) {
//这里是最小堆  所以在a[0]位置的岗哨保存了比数组中所有元素都小的元素
        H->Data[p] = H->Data[p/2];
    }
    H->Data[p] = x;
    return true;
}
struct waka{
    char *s;
    // int level;
}k[MAXDATA];
int main(){
    int N;
    char wu[5];
    scanf("%d",&N);
    MinHeap H=CreateMinHeap(N);
    for(int i=0;i<N;i++){
        scanf("%s",wu);
        if(wu[0]=='P'){
            char t[12];
            int level;
            scanf("%s %d",t,&level);
            k[level].s=(char*)malloc(sizeof(char)*12);
            k[level].s=strcpy(k[level].s,t);
            InsertMin(H,level);
        }
        else if(wu[0]=='G'){
            int r=DeleteMin(H);
            if(r!=EROOR){
                printf("%s\n",k[r].s);
            }
        }
    }
    return 0;
}
//Windows消息队列